<template>
  <div :class="{ 'container-wrap': isFullScreen }" class="chat-gpt-container">
    <!-- <div class="toggle-btn" @click="toggleFullScreen"><span v-if="isFullScreen">退出</span>全屏</div> -->
    <div :class="{ 'mobile-chat-iframe': showMobilePage }" class="chat-gpt-iframe-container">
      <iframe ref="iframe" :src="src" class="chat-gpt-iframe" frameborder="0" width="100%" allowfullscreen />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
const { chatGptUrl } = process.env;
export default {
  data() {
    return {
      isFullScreen: false,
      chatGptUrl,
      resetListener: null
    };
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo,
      'showMobilePage': state => state.app.showMobilePage
    }),
    src() {
      const avatar = encodeURIComponent(this.userInfo.avatar);
      const name = encodeURIComponent(this.userInfo.userName);
      const jobNumber = encodeURIComponent(this.userInfo.jobNumber);
      const url = `${this.chatGptUrl}?avatar=${avatar}&name=${name}&description=${jobNumber}`;
      return url;
    }
  },
  created() {
    this.resetListener = e => {
      if (e.origin === this.chatGptUrl && e.data === 'REFRESH_PAGE') {
        location.reload();
      }
    };
    window.addEventListener('message', this.resetListener);
  },
  beforeDestroy() {
    window.removeEventListener('message', this.resetListener);
  },
  methods: {
    toggleFullScreen() {
      if (!document.fullscreenElement) {
        this.requestFullscreen();
        this.isFullScreen = true;
      } else {
        if (document.exitFullscreen) {
          this.exitFullscreen();
          this.isFullScreen = false;
        }
      }
    },
    toggleExtra() {
      const sidebar = document.querySelector('.sidebar-container');
      const header = document.querySelector('.header-bg');
      const main = document.querySelector('.main-container');
      if (this.isFullScreen) {
        sidebar.style.display = 'none';
        header.style.display = 'none';
        main.style.marginLeft = '0';
      } else {
        sidebar.style.display = 'block';
        header.style.display = 'block';
        // main.style.marginLeft = '64px';
      }
    },
    requestFullscreen() {
      const element = document.documentElement;
      if (element.requestFullscreen) {
        element.requestFullscreen();
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
      }
    },
    // 退出全屏
    exitFullscreen() {
      this.$store.dispatch('ToggleSideBar');
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.chat-gpt-container {
  .container-wrap {
    padding: 20px 20px 32px;
  }
  position: relative;
  .toggle-btn {
    position: absolute;
    top: 50%;
    right: 0;
    z-index: 100;
    width: fit-content;
    padding: 0 16px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 12px;
    background: linear-gradient(162deg,#93adff 4%, #2754e2 88%) !important;
    border-radius: 16px 0px 0px 16px;
    box-shadow: 0px 3px 7px 0px #090e26;
    cursor: pointer;
  }
  .chat-gpt-iframe-container {
    width: 100%;
    height: 100vh;
    &.mobile-chat-iframe {
      height: calc(100vh - 48px);
    }
    iframe {
      height: 100%;
    }
  }
}
</style>
